﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GBlastImaging;

namespace DriveBeamEditor.Controls.General
{
    [Designer(typeof(ImageMenuDesigner))]
    public partial class ImageMenu : UserControl
    {
        #region Fields

        private Image _image;
        private bool _mouseIn;
        private bool _isSelected;

        public event EventHandler ImageChanged;
        public event EventHandler Remove;

        #endregion

        #region Properties

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public ImageMenu DropZone
        {
            get { return this; }
        }

        public Image Image
        {
            get { return _image; }
            set
            {
                if (_image == value) return;
                _image = value;
                OnImageChanged();
            }
        }

        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (_isSelected == value) return;
                _isSelected = value;
                Invalidate();
            }
        }

        [Browsable(true)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }

        [Browsable(true)]
        public override Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                base.ForeColor = value;
            }
        }

        #endregion

        #region CTORs

        public ImageMenu()
        {
            InitializeComponent();
            SetStyle(
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.DoubleBuffer |
                ControlStyles.Opaque |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.ResizeRedraw |
                ControlStyles.CacheText |
                ControlStyles.UserPaint,
                true
            );
        }

        #endregion

        #region Events

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            /* Configure graphics */
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

            e.Graphics.FillRectangle(new SolidBrush(BackColor), ClientRectangle);

            /* No image */
            if (Image == null) return;

            /* Draw image */
            using (var bitmap = new Bitmap(Image))
            {
                /* Gray out image if not selected */
                if (!IsSelected)
                    GBITreatments.TreatPicture(bitmap, GBIPixelFunctions.Sepia);

                /* Lighten if we're hovering */
                if (!_mouseIn)
                    GBITreatments.TreatPicture(bitmap, GBIPixelFunctions.AverageGrayscale);

                /* Draw image */
                var rect = new RectangleF(
                    Width / 2f - bitmap.Width / 2f,
                    Height / 3f - bitmap.Height / 2f,
                    bitmap.Width,
                    bitmap.Height);
                e.Graphics.DrawImage(bitmap, rect);

                /* Draw text */
                if (string.IsNullOrEmpty(Text)) return;
                var sz = e.Graphics.MeasureString(Text, Font);
                e.Graphics.DrawString(
                    Text,
                    Font,
                    new SolidBrush(ForeColor),
                    new RectangleF(
                        Width / 2f - sz.Width / 2f,
                        Height - sz.Height,
                        sz.Width,
                        sz.Height));

                /* Resize if needed */
                if (sz.Width > Width || sz.Height > Height)
                {
                    Size = new Size(
                        (int)Math.Max(sz.Width, Width),
                        (int)Math.Max(sz.Height, Height)
                    );
                }
            }
        }

        private void ImageMenu_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && Remove != null)
                Remove(this, new EventArgs());
        }

        #endregion

        #region Methods

        protected void OnImageChanged()
        {
            if (ImageChanged != null)
                ImageChanged(this, new EventArgs());

            Invalidate();
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            _mouseIn = true;
            Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            _mouseIn = false;
            Invalidate();
        }

        #endregion
    }
}
